@echo off
chcp 65001 >nul
echo ========================================
echo AIGC-EDU Launcher Build Script
echo ========================================
echo.

:: Check if Python is installed
python --version >nul 2>&1
if errorlevel 1 (
    echo Error: Python not found, please install Python 3.7+
    pause
    exit /b 1
)

echo Detected Python version:
python --version
echo.

:: Check if pip is available
pip --version >nul 2>&1
if errorlevel 1 (
    echo Error: pip not found, please check Python installation
    pause
    exit /b 1
)

:: Create virtual environment (optional)
echo Do you want to create a virtual environment? (y/n)
set /p create_venv=
if /i "%create_venv%"=="y" (
    echo Creating virtual environment...
    python -m venv venv
    call venv\Scripts\activate.bat
    echo Virtual environment activated
    echo.
)

:: Install dependencies
echo Installing dependencies...
pip install -r requirements.txt
if errorlevel 1 (
    echo Error: Dependency installation failed
    pause
    exit /b 1
)
echo Dependencies installed successfully
echo.

:: Clean previous builds
echo Cleaning previous build files...
if exist "dist" rmdir /s /q "dist"
if exist "build" rmdir /s /q "build"
echo Cleanup completed
echo.

:: Start packaging
echo Starting application packaging...
echo Using PyInstaller config file: main.spec
pyinstaller main.spec

if errorlevel 1 (
    echo.
    echo Error: Packaging failed, trying basic command packaging...
    echo.
    pyinstaller --onefile --windowed --name="AIGC-EDU-Launcher" main.py
    if errorlevel 1 (
        echo Packaging still failed, please check error messages
        pause
        exit /b 1
    )
)

echo.
echo ========================================
echo Packaging completed!
echo ========================================
echo.

:: Check output file
if exist "dist\AIGC-EDU-Launcher.exe" (
    echo Executable file location: dist\AIGC-EDU-Launcher.exe
    echo File size:
    dir "dist\AIGC-EDU-Launcher.exe" | find ".exe"
    echo.
    echo Do you want to run a test? (y/n)
    set /p run_test=
    if /i "%run_test%"=="y" (
        echo Starting test...
        start "" "dist\AIGC-EDU-Launcher.exe"
    )
) else (
    echo Warning: Output exe file not found
    echo Please check files in dist directory
    dir dist
)

echo.
echo Build script execution completed
echo Press any key to exit...
pause >nul